package cn.itcast.xuesheng.service.impl;

import cn.itcast.xuesheng.dao.UserDao;
import cn.itcast.xuesheng.dao.impl.UserDaoImpl;
import cn.itcast.xuesheng.domain.User;
import cn.itcast.xuesheng.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
//    public void setUserDao(UserDaoImpl userDao) {
//    }
    public User isLogin(User user) {
//        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//        userDao = (UserDao) applicationContext.getBean("userDao");
//        UserDaoImpl userDao = new UserDaoImpl();
        User user1 = userDao.isLogin(user);
        return user1;
    }

    public void insert(User user) {
//        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//        UserDao userDao = (UserDao) applicationContext.getBean("userDao");
//        UserDaoImpl userDao = new UserDaoImpl();
        userDao.insert(user);
    }

    @Override
    public User check(String username) {
//        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//        UserDao userDao = (UserDao) applicationContext.getBean("userDao");
//        UserDaoImpl userDao = new UserDaoImpl();
        User checkuser = userDao.check(username);
        return checkuser;
    }

    @Override
    public void update(User user) {
//        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//        UserDao userDao = (UserDao) applicationContext.getBean("userDao");
//        UserDaoImpl userDao = new UserDaoImpl();
        userDao.update(user);
    }

    @Override
    public void delete(String tid) {
//        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//        UserDao userDao = (UserDao) applicationContext.getBean("userDao");
//        UserDaoImpl userDao = new UserDaoImpl();
        userDao.delete(Integer.parseInt(tid));
    }

    @Override
    public void deleteSelect(String[] id) {
//        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//        UserDao userDao = (UserDao) applicationContext.getBean("userDao");
//        UserDaoImpl userDao=new UserDaoImpl();
        for (String string:id){
            userDao.delete(Integer.parseInt(string));
        }
    }

}
